package com.zhiche.lisa.bms.controller.utils;

import com.alibaba.fastjson.JSONObject;
import com.lisa.json.JSONUtil;
import com.lisa.syslog.SysLogUtil;
import com.lisa.syslog.model.SysLogDataPo;
import com.lisa.syslog.model.publics.PublicLog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author linbao
 * @date 2019-11-08
 */
@Component
public class ToolLogUtils {

    @Value("${toolSyslogSaveFlag}")
    private boolean saveFlag;

    /**
     * 记录日志
     *
     * @param sourceSystem     来源系统-调用接口方
     * @param functionApi      目标api-接口名称
     * @param businessEventKey 数据id
     * @param dataParam        参数
     * @param statue           状态 1-成功; -1忽略; 0-待处理
     * @return
     */
    public PublicLog addToolSystemLog(String sourceSystem, String functionApi, String businessEventKey, Object dataParam, Integer statue, String errorMsg) {
        PublicLog publicLog = new PublicLog();
        // 来源系统
        publicLog.setSourceSystem(sourceSystem);
        // 目标系统
        publicLog.setTargetSystem("lisa-bms");
        // 目标接口api
        publicLog.setFuncIdx(functionApi);
        // 数据id
        publicLog.setBusEventKey(businessEventKey);
        // 参数
        if (Objects.nonNull(dataParam)) {
            publicLog.setReqParam(JSONObject.toJSONString(dataParam));
        }
        // 状态
        publicLog.setDataStatus(statue);
        // 执行次数
        publicLog.setExecCnt(1);
        // 执行错误
        publicLog.setErrCont(errorMsg);

        // String key = publicLog.getSourceSystem() + "_" + publicLog.getTargetSystem();
        // String fkye = publicLog.getFuncIdx() + "_" + publicLog.getBusEventKey() + "_" + publicLog.getDataStatus();
        // // 转换
        // SysLogDataPo sysLogDataPo = new SysLogDataPo(SysLogUtil.PUBLIC_, JSONUtil.toJsonStr(publicLog));
        // SysLogUtil.addSysLogs(key, fkye, sysLogDataPo, Boolean.TRUE);
        this.commonAddLog(publicLog);
        return publicLog;
    }

    /**
     * 新增成功处理日志
     *
     * @param publicLog
     */
    public void addSuccessSystemLog(PublicLog publicLog) {
        if (Objects.nonNull(publicLog)) {
            publicLog.setDataStatus(1);
            this.commonAddLog(publicLog);
        }
    }

    /**
     * 新增失败的日志
     *
     * @param publicLog
     * @param errorMsg
     */
    public void addErrorSystmeLog(PublicLog publicLog, String errorMsg) {
        if (Objects.nonNull(publicLog)) {
            publicLog.setDataStatus(-1);
            // 执行错误
            publicLog.setErrCont(errorMsg);
            this.commonAddLog(publicLog);
        }
    }

    /**
     * 保存日志
     *
     * @param publicLog
     */
    private void commonAddLog(PublicLog publicLog) {
        String key = publicLog.getSourceSystem() + "_" + publicLog.getTargetSystem();
        String fkye = publicLog.getFuncIdx() + "_" + publicLog.getBusEventKey() + "_" + publicLog.getDataStatus();
        // 转换
        SysLogDataPo sysLogDataPo = new SysLogDataPo(SysLogUtil.PUBLIC_, JSONUtil.toJsonStr(publicLog));
        SysLogUtil.addSysLogs(key, fkye, sysLogDataPo, saveFlag);
    }
}
